package com.program.facesigninsystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.program.facesigninsystem.dao.IRewardsDao;
import com.program.facesigninsystem.entity.Product;
import com.program.facesigninsystem.entity.Rewards;
import com.program.facesigninsystem.iteration.entity.RewardPointsEntity;
import com.program.facesigninsystem.iteration.mapper.RewardPointsMapper;
import com.program.facesigninsystem.service.IRewardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class RewardService implements IRewardService {
    @Autowired
    IRewardsDao iRewardsDao;
    @Resource
    RewardPointsMapper rewardPointsMapper;

    @Override
    public Rewards selectReward(String uid) {
        RewardPointsEntity rewardPointsEntity = rewardPointsMapper.selectOne(new QueryWrapper<RewardPointsEntity>().eq("uid", uid));
        Rewards rewards = new Rewards();
        if (rewardPointsEntity != null) {
            rewards.setUid(uid)
                    .setLimit_from_record_a(rewardPointsEntity.getLimitFromRecordA())
                    .setLimit_from_record_b(rewardPointsEntity.getLimitFromRecordB())
                    .setLimit_from_record_c(rewardPointsEntity.getLimitFromRecordC())
                    .setLimit_from_activity(rewardPointsEntity.getLimitFromActivity())
                    .setLast_gain_day(rewardPointsEntity.getLastGainDay())
                    .setReward_points(rewardPointsEntity.getRewardPoints())
                    .setDay_reward(rewardPointsEntity.getDayReward());
        }else {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            rewards.setUid(uid)
                    .setLimit_from_record_a(0)
                    .setLimit_from_record_b(0)
                    .setLimit_from_record_c(0)
                    .setLimit_from_activity(0)
                    .setLast_gain_day(simpleDateFormat.format(new Date()))
                    .setReward_points(0)
                    .setDay_reward(0);

            addNewRewards(uid, 0, 0, 0, 1,
                    simpleDateFormat.format(new Date()), 0, 0);
        }

        return rewards;
    }

    @Override
    public void addNewRewards(String uid, int limit_from_record_a, int limit_from_record_b, int limit_from_record_c, int limit_from_record_activity, String last_gain_day, int reward_points,int day_ward) {
        iRewardsDao.addNewRewards(uid,limit_from_record_a,limit_from_record_b,limit_from_record_c,limit_from_record_activity,last_gain_day,reward_points,day_ward);
    }

    @Override
    public void updateRewards(String uid, int limit_from_record_a, int limit_from_record_b, int limit_from_record_c, int limit_from_record_activity, String last_gain_day, int reward_points,int day_ward) {
        iRewardsDao.updateRewards(uid,limit_from_record_a,limit_from_record_b,limit_from_record_c,limit_from_record_activity,last_gain_day,reward_points,day_ward);
    }

    @Override
    public void addNewProducts(String id, String name, int price, String caption, String imageUrl, int storage, int total) {
        iRewardsDao.addNewProducts(id, name, price, caption, imageUrl, storage, total);
    }

    @Override
    public void updateProductShow(int show,String id) {
        iRewardsDao.updateProductShow(show,id);
    }

    @Override
    public void updateProductsStorage(String id, int storage) {
        iRewardsDao.updateProductsStorage(id, storage);
    }

    @Override
    public List<Product> selectAllProducts() {
        return iRewardsDao.selectAllProducts();
    }

    @Override
    public List<Product> selectAllProductsShow() {
        return iRewardsDao.selectAllProductsShow();
    }

    @Override
    public void addNewRedeemRecord(String id, String uid, String name, String phone, String createTime, String province, String city, String address, String pid, int state) {
        iRewardsDao.addNewRedeemRecord(id, uid, name, phone, createTime, province, city, address, pid, state);
    }

    @Override
    public void updateRedeemRecordState(String id, int state) {
        iRewardsDao.updateRedeemRecordState(id, state);
    }

    @Override
    public Map<String,Integer> selectProductById(String id) {
        return iRewardsDao.selectProductById(id);
    }

    @Override
    public List<Map<String,Object>> selectAllRedeemRecord() {
        return iRewardsDao.selectAllRedeemRecord();
    }

    @Override
    public List<Map<String,Object>> selectRecordNotSend() {
        return iRewardsDao.selectRecordNotSend();
    }

    @Override
    public List<Map<String,Object>> selectRecordAlreadySend() {
        return iRewardsDao.selectRecordAlreadySend();
    }

    @Override
    public List<Map<String,Object>> selectMyRedeemRecord(String uid) {
        return iRewardsDao.selectMyRedeemRecord(uid);
    }
}
